package com.qifan.alb2b.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;
import com.cqcis.base.orm.jdbc.JdbcUtil;
import com.qifan.alb2b.domain.CountryInfoDomain;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.Page;

/**
 * 目的地CURD
 * @author hc24
 *
 */
@Repository
public class CountryInfoDao extends BaseDao {
	private final static Log logger=LogFactory.getLog(CountryInfoDao.class);
	
	/**
	 * 根据条件分页查询国家信息
	 * @param dgm 分页参数
	 * @param cid 国家查询参数
	 * @return Page对象（分页信息&分页数据）
	 */
	public Page query(DataGridModel dgm,CountryInfoDomain cid){
		logger.debug("查询国家");
		/* 构建sql */
		StringBuilder sql=new StringBuilder();
		List<Object> params=new ArrayList<Object>();
		sql.append("select * from countryinfo where 1=1");
		if(cid.getCountryCm()!=null&&!"".equals(cid.getCountryCm())){
			sql.append(" and countryCm like ?");
			params.add("%"+cid.getCountryCm()+"%");
		}
		if(cid.getStatus()!=-1){
			sql.append(" and status=?");
			params.add(cid.getStatus());
		}
		if(dgm.getSort()!=null&&!"".equals(dgm.getSort()))//判断是否排序
			sql.append(" order by ").append(dgm.getSort()).append(" ").append(dgm.getOrder());
		return super.queryObjByPage(sql.toString(), dgm.getRows(), dgm.getPage(), params, CountryInfoDomain.class);
	}
	
	public CountryInfoDomain getCountryById(String cid){
		String sql="select * from countryInfo where countryId=?";
		List<CountryInfoDomain> list=JdbcUtil.queryForList(this, CountryInfoDomain.class, sql, cid);
		if(list.size()>0)
			return list.get(0);
		return null;
	}
	
	public void add(CountryInfoDomain CountryInfoDomain){
		super.addObj(CountryInfoDomain);
	}
	
	public void update(CountryInfoDomain CountryInfoDomain){
		super.updateObj(CountryInfoDomain, "countryId");
	}
	
	public void delete(String[] ids){
		StringBuilder delSql=new StringBuilder();
		delSql.append("delete from countryInfo where countryId in(");
		for(int i=0;i<ids.length-1;i++){
			delSql.append("?,");
		}
		delSql.append("?)");
		this.getJdbcTemplate().update(delSql.toString(), ids);
	}
}
